import 'package:cached_network_image/cached_network_image.dart';
import 'package:data_plugin/bmob/bmob.dart';
import 'package:data_plugin/bmob/bmob_query.dart';
import 'package:data_plugin/bmob/table/bmob_object.dart';
import 'package:flutter_elf/_base/common_pull_to_refresh.dart';
import 'package:flutter_elf/_base/common_scaffold_list.dart';
import 'package:flutter_elf/base/base_page.dart';
import 'package:flutter_elf/config/component_index.dart';
import 'package:flutter_elf/model/gank_io_girl_response.dart';
import 'package:flutter_elf/network/service/gank_io_service.dart';
import 'package:flutter_elf/page/common/JsonUtils.dart';
import 'package:flutter_elf/page/forest/ant_forest_page.dart';
import 'package:flutter_elf/page/hotfix/hot_fix_page.dart';
import 'package:flutter_elf/page/huwei_weather_page.dart';
import 'package:flutter_elf/page/popup_window.dart';
import 'package:flutter_elf/page/salary_page.dart';
import 'package:flutter_elf/page/scroll/custom_scroll.dart';
import 'package:flutter_elf/page/select_emoj_page.dart';
import 'package:flutter_elf/page/slide_unlock/slide_unlock_page.dart';
import 'package:flutter_elf/page/slide_vertification_page.dart';
import 'package:flutter_elf/page/theme_page.dart';
import 'package:flutter_elf/page/topic_page.dart';
import 'package:flutter_elf/page/translate_generate_page.dart';
import 'package:flutter_elf/page/turn_book/BookPage.dart';
import 'package:flutter_elf/page/turntable/pie_chart_page.dart';
import 'package:flutter_elf/page/turntable/turn_table_widget.dart';
import 'package:flutter_elf/page/tv/tv_live_page.dart';
import 'package:flutter_elf/page/video/main.dart';
import 'package:flutter_elf/page/xinge_push.dart';
import 'package:flutter_elf/page/zefyr/zefyr_page.dart';
import 'package:flutter_elf/page/zhi_yao_detail_page.dart';
import 'package:flutter_elf/page/zhihu_viewpage.dart';
import 'package:flutter_elf/utils/object_util.dart';
import 'package:flutter_elf/utils/toast_utils.dart';
import 'package:flutter_elf/widgets/common_widgets.dart';
import 'CustomToastPage.dart';
import 'TextSpanPage.dart';
import 'app_info_page.dart';
import 'base/BaseWidgetTest.dart';
import 'base/StatefulTest.dart';
import 'base/did_pop_next_page.dart';
import 'bmob/first.dart';
import 'bmob/second.dart';
import 'common/WebViewTest.dart';
import 'common/taurus_page.dart';
import 'common/webview_page.dart';
import 'db/database_page.dart';
import 'db/db_page.dart';
import 'dialog_test/dialog_test_page.dart';
import 'example/page_storage_example.dart';
import 'gank_io.dart';
import 'gank_io2.dart';
import 'html/html_eidt.dart';
import 'html_page.dart';
import 'isolate/isolate_page.dart';
import 'life_cycle_page.dart';
import 'm3u8_download_page.dart';
import 'maomao.dart';
import 'mark_down_page.dart';
import 'overlay_page.dart';
import 'package:markdown/markdown.dart' as Markdown;

///界面汇总

class SummaryPage extends BaseWidget {
  @override
  BaseWidgetState<BaseWidget> getState() {
    // TODO: implement getState
    return SummaryPageState();
  }

}
final doc =
    '[{"insert":"Zefyr"},{"insert":"\n","attributes":{"heading":1}},{"insert":"Soft and gentle rich text editing for Flutter applications.","attributes":{"i":true}},{"insert":"\n"},{"insert":"​","attributes":{"embed":{"type":"image","source":"asset://images/breeze.jpg"}}},{"insert":"\n"},{"insert":"Photo by Hiroyuki Takeda.","attributes":{"i":true}},{"insert":"\nZefyr is currently in "},{"insert":"early preview","attributes":{"b":true}},{"insert":". If you have a feature request or found a bug, please file it at the "},{"insert":"issue tracker","attributes":{"a":"https://github.com/memspace/zefyr/issues"}},{"insert":'
    '".\nDocumentation"},{"insert":"\n","attributes":{"heading":3}},{"insert":"Quick Start","attributes":{"a":"https://github.com/memspace/zefyr/blob/master/doc/quick_start.md"}},{"insert":"\n","attributes":{"block":"ul"}},{"insert":"Data Format and Document Model","attributes":{"a":"https://github.com/memspace/zefyr/blob/master/doc/data_and_document.md"}},{"insert":"\n","attributes":{"block":"ul"}},{"insert":"Style Attributes","attributes":{"a":"https://github.com/memspace/zefyr/blob/master/doc/attr'
    'ibutes.md"}},{"insert":"\n","attributes":{"block":"ul"}},{"insert":"Heuristic Rules","attributes":{"a":"https://github.com/memspace/zefyr/blob/master/doc/heuristics.md"}},{"insert":"\n","attributes":{"block":"ul"}},{"insert":"FAQ","attributes":{"a":"https://github.com/memspace/zefyr/blob/master/doc/faq.md"}},{"insert":"\n","attributes":{"block":"ul"}},{"insert":"Clean and modern look"},{"insert":"\n","attributes":{"heading":2}},{"insert":"Zefyr’s rich text editor is built with simplicity and fle'
    'xibility in mind. It provides clean interface for distraction-free editing. Think Medium.com-like experience.\nMarkdown inspired semantics"},{"insert":"\n","attributes":{"heading":2}},{"insert":"Ever needed to have a heading line inside of a quote block, like this:\nI’m a Markdown heading"},{"insert":"\n","attributes":{"block":"quote","heading":3}},{"insert":"And I’m a regular paragraph"},{"insert":"\n","attributes":{"block":"quote"}},{"insert":"Code blocks"},{"insert":"\n","attributes":{"headin'
    'g":2}},{"insert":"Of course:\nimport ‘package:flutter/material.dart’;"},{"insert":"\n","attributes":{"block":"code"}},{"insert":"import ‘package:zefyr/zefyr.dart’;"},{"insert":"\n\n","attributes":{"block":"code"}},{"insert":"void main() {"},{"insert":"\n","attributes":{"block":"code"}},{"insert":" runApp(MyZefyrApp());"},{"insert":"\n","attributes":{"block":"code"}},{"insert":"}"},{"insert":"\n","attributes":{"block":"code"}},{"insert":"\n\n\n"}]';

class SummaryPageState extends BaseWidgetState<SummaryPage> {


  @override
  void onCreate(){
    super.onCreate();
    Bmob.init("https://api2.bmob.cn", "d6120f0280df61012ca8e02b96e3d3b1", "27fd92eaacf8881933b09af7a4d8dc30");
    html2markdown();
  }

  html2markdown(){
    String s = Markdown.markdownToHtml(doc);
    print('sssssssssssssssssssssssssss');
    print(s);
  }

  buildItem(String title, Widget widget) {
    return GestureDetector(
      onTap: () => NavigatorUtils.navigatorRouter(context, widget),
      child: Container(
        width: double.infinity,
        alignment: Alignment.center,
        height: px(100),
        decoration:
            BoxDecoration(border: Border.all(color: Colors.black, width: 1)),
        child: Text(title),
      ),
    );
  }


  @override
  Widget buildView(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      body: ListView(
        children: <Widget>[
          buildItem('下载m3u8', M3u8DownloadPage()),
          buildItem('ScrollableDemo', ScrollableDemo()),
          buildItem('SalaryPage', SalaryPage()),
          buildItem('PageStoragePage', PageStoragePage()),
          buildItem('电视直播', TvLivePage()),
          buildItem('Dialog测试', DialogTestPage()),
          buildItem('MyVideoPage', MyVideoPage()),
          buildItem('嵌入在界面中的CommonList', GankIO2Page()),
          buildItem('ZefyrPage', ZefyrPage()),
          buildItem('HtmlEditPage', HtmlEditPage(title:'title')),
          buildItem('PopWindowPage', PopWindowPage(title:'title')),
          buildItem('ZhihuPage', ZhihuPage()),
          buildItem('MyPages', MyPages()),
          buildItem('HtmlPage', HtmlPage()),
          buildItem('加载本地html文件', WebviewLoadHtml()),
          buildItem('MarkDownPage', MarkDownPage()),
          buildItem('maomao', AnimationHomeWidget()),
          buildItem('信鸽推送', XingePushPage()),
          buildItem('BmobFirstPage', BmobFirstPage()),
          buildItem('BmobSecondPage', SecondBmobPage()),
          buildItem('华为天气', HuaWeiWeatherPage()),
          buildItem('JsonWidget', JsonWidget()),
          buildItem('LifeCyclePage', LifeCyclePage()),
          buildItem('加载html',
              WebViewPage('http://www.baidu.com')),
          buildItem('冲上云霄样式', TaurusPage()),
          buildItem('腾讯浏览器内核', WebViewTeState()),
          buildItem('修改主题', ThemePage()),
          buildItem('overlay', OverlayPage()),
          buildItem('滑动验证', SlideVerifyPage()),
          buildItem('仿收能量', AntForestPage()),
          buildItem('大转盘', PieChartPage()),
          buildItem('自己的大转盘', TurnTablePage()),
          buildItem('滑动解锁', SlideUnlockPage()),
          buildItem('RawGestureDetector', BookPage()),

          Text('性能比较'),
          buildItem('系统', StatefulTest()),
          buildItem('封装', BaseWidgetTest()),
          buildItem('数据库', DBPage()),
          buildItem('封装数据库', DatabasePage()),
          GestureDetector(
            onTap: () {
              NavigatorUtils.navigatorRouter(context, BaseCommonRefreshPage<WidgetItem,GankIOGirlResponse,GankIOGirlData>(
                title: '哈哈,我要测试',
                top: Text('标题'),
                getItem: (dynamic info){
                  globalPrint('=========info' + info.toString());
                  GankIOGirlData data = info as GankIOGirlData;
                  return MeizhiItem<GankIOGirlData>(data.images[0]);
                },
                limit: 20,
                futureData: (int page){
                  return futureData(page);
                },
              ));
            },
            child: Container(
              width: double.infinity,
              alignment: Alignment.center,
              height: px(100),
              decoration:
              BoxDecoration(border: Border.all(color: Colors.black, width: 1)),
              child: Text('干货'),
            ),
          ),

          buildItem('修复', HotFixPage()),
          buildItem('App信息', AppInfoPage()),
          buildItem('didPopNext', DidPopNextPage()),
          buildItem('翻译', TranslateGeneratePage()),
          buildItem('自定义Toast', CustomToastPage()),
          buildItem('isolate', IsolatePage()),
          buildItem('主题', TopicPage()),
          buildItem('选择emoj表情', SelectEmojPage()),
          buildItem('TextSpan带表情', TextSpanPage()),
          buildButton('知妖', (){
            toZhiYao();
          }),
        ],
      ),
    );
  }

  toZhiYao(){
    NavigatorUtils.navigatorRouter(context, CommonListPage<WidgetItem,ZhiYaoModel,TitleModel>(
      title: '哈哈,我要测试',
      top: Text('标题'),
      getItem: (dynamic info){
        globalPrint('=========info' + info.toString());
        TitleModel data = info as TitleModel;
        return ZhiYaoItem(data,(){
          showToast('====');
          NavigatorUtils.navigatorRouter(context, ZhiYaoDetailPage(data));
        });
      },
      limit: 20,
      futureData: (int page){
        return zhiYaoFuture(page);
      },
    ));
  }

  Future<GankIOGirlResponse> futureData(int page){
    return GankIOService.getCategory(page, state: this);
  }

  @override
  PreferredSizeWidget initAppbar() {
    // TODO: implement initAppbar
    return null;
  }
}


class ZhiYaoItem extends StatelessWidget{
  final TitleModel model;
  VoidCallback onTap;
  ZhiYaoItem(this.model,VoidCallback onTap);
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return GestureDetector(
      onTap: (){
        NavigatorUtils.navigatorRouter(context, ZhiYaoDetailPage(model));
      },
      child: Container(
          child:Text(model.title)
      ),
    );
  }

}

Future<ZhiYaoModel> zhiYaoFuture(int page)async{
  BmobQuery<TitleModel> query = BmobQuery();
  query.setLimit(20) ;
  query.setSkip(20 * page);//0 500 1000 1500
  List<dynamic> data = await query.queryObjects();
  List<TitleModel> models = data.map((d){
    return TitleModel.fromJson(d);
  }).toList();
  return Future<ZhiYaoModel>.value(ZhiYaoModel(models));
}

class ZhiYaoModel{
  List<TitleModel> data;
  ZhiYaoModel(this.data);
}

class TitleModel extends BmobObject {
  String title;String url;
  TitleModel(this.title, this.url);
  TitleModel.fromJson(jsonRes){
    this.url = jsonRes['url'];
    this.title = jsonRes['title'];
  }
  isEmpty(){
    return ObjectUtil.isEmptyString(this.title) || ObjectUtil.isEmptyString(this.url);
  }
  TitleModel.empty();
  @override
  Map getParams() {
    Map<String,String> map = {
      'title':this.title,
      'url':this.url,
      'objectId':this.objectId,
    };
    return map;
  }
}

class MeizhiItem<GankIOGirlData> extends StatelessWidget{
  final String url;
  MeizhiItem(this.url);
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Container(
      child: CachedNetworkImage(
        imageUrl: url,
      )
    );
  }
}
